1
|
|
|
/** global: jest */ |
2
|
|
|
/* global jest describe test expect beforeAll afterAll */ |
3
|
|
|
|
4
|
|
|
const fs = require('fs'); |
5
|
|
|
// Load local environments |
6
|
|
|
if (fs.existsSync(`${__dirname}/../.env`)) { |
7
|
|
|
require('node-env-file')(`${__dirname}/../.env`); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
const Server = require('../src/server'); |
11
|
|
|
const User = require('../src/repository/User'); |
12
|
|
|
const Status = require('../src/repository/Status'); |
13
|
|
|
const config = require('../src/config/server.config'); |
14
|
|
|
const moment = require('moment'); |
15
|
|
|
|
16
|
|
|
jest.dontMock('console'); |
17
|
|
|
|
18
|
|
|
const serverPromise = Server.start(); |
19
|
|
|
|
20
|
|
|
const user1 = { |
21
|
|
|
username: 'testuser', |
22
|
|
|
password: 'testuser', |
23
|
|
|
}; |
24
|
|
|
|
25
|
|
|
describe('User', () => { |
26
|
|
|
let id; |
27
|
|
|
const url = `${config.url.apiPrefix}/login`; |
28
|
|
|
|
29
|
|
|
beforeAll(() => User.add(user1).then((result) => { |
30
|
|
|
id = result.data[0]._id; |
31
|
|
|
})); |
32
|
|
|
|
33
|
|
|
test('login success', () => { |
34
|
|
|
return serverPromise.then((server) => { |
35
|
|
|
return server.inject({ url, method: 'POST', payload: user1 }).then((response) => { |
36
|
|
|
expect(response.statusCode).toBe(200); |
37
|
|
|
const payload = JSON.parse(response.payload); |
38
|
|
|
expect(payload).toBeDefined(); |
39
|
|
|
expect(payload.success).toBe(true); |
40
|
|
|
}); |
41
|
|
|
}); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
test('login fail', () => { |
45
|
|
|
return serverPromise.then((server) => { |
46
|
|
|
return server.inject({ url, method: 'POST', payload: { username: 'testuser', password: 'no-testuser' } }).then((response) => { |
47
|
|
|
expect(response.statusCode).toBe(401); |
48
|
|
|
const payload = JSON.parse(response.payload); |
49
|
|
|
expect(payload).toBeDefined(); |
50
|
|
|
expect(payload.success).toBe(false); |
51
|
|
|
}); |
52
|
|
|
}); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
afterAll(() => User.remove(id)); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
const statusDataForAllVersion = { |
59
|
|
|
type: 'routineInspection', |
60
|
|
|
deviceTypes: ['android', 'ios'], |
61
|
|
|
contents: 'routineInspection-test1', |
62
|
|
|
isActivated: true, |
63
|
|
|
deviceSemVersion: '*', |
64
|
|
|
appSemVersion: '*', |
65
|
|
|
startTime: new Date(), |
66
|
|
|
endTime: new Date(new Date().getTime() + 3600000), |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
const statusDataForRangedVersion = { |
70
|
|
|
type: 'routineInspection', |
71
|
|
|
deviceTypes: ['android'], |
72
|
|
|
contents: 'routineInspection-test2', |
73
|
|
|
isActivated: true, |
74
|
|
|
deviceSemVersion: '>=1.2.3 < 3.2.1', |
75
|
|
|
appSemVersion: '>=1.2.3 < 3.2.1', |
76
|
|
|
startTime: new Date(), |
77
|
|
|
endTime: new Date(new Date().getTime() + 3600000), |
78
|
|
|
}; |
79
|
|
|
|
80
|
|
|
const check1 = { deviceType: 'android', deviceVersion: '1.2.2', appVersion: '3.3.3' }; |
81
|
|
|
const check2 = { deviceType: '*', deviceVersion: '*', appVersion: '*' }; |
82
|
|
|
const check3 = { deviceType: 'paper', deviceVersion: '1.2.3', appVersion: '3.4.5' }; |
83
|
|
|
const check4 = { deviceType: '*', deviceVersion: 'invalid-version', appVersion: 'invalid-version' }; |
84
|
|
|
|
85
|
|
|
const statusDataToBeAdded = { |
86
|
|
|
type: 'routineInspection', |
87
|
|
|
device_types: ['android', 'ios'], |
88
|
|
|
contents: 'routineInspection-added1', |
89
|
|
|
is_activated: true, |
90
|
|
|
device_sem_version: '*', |
91
|
|
|
app_sem_version: '*', |
92
|
|
|
start_time: moment().format(), |
93
|
|
|
end_time: moment().add(2, 'hours').format(), |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
const statusDataToBeUpdated = { |
97
|
|
|
type: 'routineInspection', |
98
|
|
|
device_types: ['paper', 'ios'], |
99
|
|
|
contents: 'routineInspection-added1-updated1', |
100
|
|
|
is_activated: true, |
101
|
|
|
device_sem_version: '>=1.2.3', |
102
|
|
|
app_sem_version: '>=4.5.6 || =1.2.3', |
103
|
|
|
start_time: moment().format(), |
104
|
|
|
end_time: moment().add(3, 'hours').format(), |
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
describe('status', () => { |
108
|
|
|
describe('checkStatus', () => { |
109
|
|
|
let ids = []; |
110
|
|
|
|
111
|
|
|
const makeUrl = check => `${config.url.statusApiPrefix}/check?device_type=${check.deviceType}&device_version=${check.deviceVersion}&app_version=${check.appVersion}`; |
112
|
|
|
|
113
|
|
|
beforeAll(() => { |
114
|
|
|
return Promise.all([ |
115
|
|
|
Status.add(statusDataForAllVersion), |
116
|
|
|
Status.add(statusDataForRangedVersion), |
117
|
|
|
]).then((results) => { |
118
|
|
|
ids = results.map(result => result.data[0]._id); |
119
|
|
|
}); |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
test('matches one', () => { |
123
|
|
|
return serverPromise.then((server) => { |
124
|
|
|
return server.inject({ url: makeUrl(check1) }).then((response) => { |
125
|
|
|
expect(response.statusCode).toBe(200); |
126
|
|
|
const payload = JSON.parse(response.payload); |
127
|
|
|
expect(payload.success).toBe(true); |
128
|
|
|
expect(payload.data).toBeDefined(); |
129
|
|
|
expect(payload.data.length).toBeGreaterThan(0); |
130
|
|
|
expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeTruthy(); |
131
|
|
|
expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeFalsy(); |
132
|
|
|
}); |
133
|
|
|
}); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
test('matches all', () => { |
137
|
|
|
return serverPromise.then((server) => { |
138
|
|
|
return server.inject({ url: makeUrl(check2) }).then((response) => { |
139
|
|
|
expect(response.statusCode).toBe(200); |
140
|
|
|
const payload = JSON.parse(response.payload); |
141
|
|
|
expect(payload.success).toBe(true); |
142
|
|
|
expect(payload.data).toBeDefined(); |
143
|
|
|
expect(payload.data.length).toBeGreaterThanOrEqual(2); |
144
|
|
|
expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeTruthy(); |
145
|
|
|
expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeTruthy(); |
146
|
|
|
}); |
147
|
|
|
}); |
148
|
|
|
}); |
149
|
|
|
|
150
|
|
|
test('matches nothing', () => { |
151
|
|
|
return serverPromise.then((server) => { |
152
|
|
|
return server.inject({ url: makeUrl(check3) }).then((response) => { |
153
|
|
|
expect(response.statusCode).toBe(200); |
154
|
|
|
const payload = JSON.parse(response.payload); |
155
|
|
|
expect(payload.success).toBe(true); |
156
|
|
|
expect(payload.data).toBeDefined(); |
157
|
|
|
expect(payload.data.some(item => statusDataForAllVersion.contents === item.contents)).toBeFalsy(); |
158
|
|
|
expect(payload.data.some(item => statusDataForRangedVersion.contents === item.contents)).toBeFalsy(); |
159
|
|
|
}); |
160
|
|
|
}); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
test('invalid version', () => { |
164
|
|
|
return serverPromise.then((server) => { |
165
|
|
|
return server.inject({ url: makeUrl(check4) }).then((response) => { |
166
|
|
|
expect(response.statusCode).toBe(400); |
167
|
|
|
const payload = JSON.parse(response.payload); |
168
|
|
|
expect(payload.code).toBe(400100); |
169
|
|
|
expect(payload.success).toBe(false); |
170
|
|
|
}); |
171
|
|
|
}); |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
|
afterAll(() => Promise.all(ids.map(id => Status.remove(id)))); |
175
|
|
|
}); |
176
|
|
|
|
177
|
|
|
describe('status CRUD', () => { |
178
|
|
|
const url = config.url.statusApiPrefix; |
179
|
|
|
|
180
|
|
|
test('add status', () => { |
181
|
|
|
return serverPromise.then((server) => { |
182
|
|
|
return server.inject({ |
183
|
|
|
url, |
184
|
|
|
method: 'POST', |
185
|
|
|
payload: statusDataToBeAdded, |
186
|
|
|
credentials: { username: 'admin' }, |
187
|
|
|
}).then((response) => { |
188
|
|
|
expect(response.statusCode).toBe(200); |
189
|
|
|
const payload = JSON.parse(response.payload); |
190
|
|
|
expect(payload.success).toBe(true); |
191
|
|
|
expect(payload.data[0].contents).toBe(statusDataToBeAdded.contents); |
192
|
|
|
statusDataToBeAdded.id = payload.data[0].id; |
193
|
|
|
}); |
194
|
|
|
}); |
195
|
|
|
}); |
196
|
|
|
|
197
|
|
|
test('update status', () => { |
198
|
|
|
return serverPromise.then((server) => { |
199
|
|
|
return server.inject({ |
200
|
|
|
url: `${url}/${statusDataToBeAdded.id}`, |
201
|
|
|
method: 'PUT', |
202
|
|
|
payload: statusDataToBeUpdated, |
203
|
|
|
credentials: { username: 'admin' }, |
204
|
|
|
}).then((response) => { |
205
|
|
|
expect(response.statusCode).toBe(200); |
206
|
|
|
const payload = JSON.parse(response.payload); |
207
|
|
|
expect(payload.success).toBe(true); |
208
|
|
|
expect(payload.data[0].id).toBe(statusDataToBeAdded.id); |
209
|
|
|
return payload; |
210
|
|
|
}).then(payload => Status.find({ _id: payload.data[0].id })) |
211
|
|
|
.then((result) => { |
212
|
|
|
expect(result.length).toBe(1); |
213
|
|
|
expect(result[0].contents).toBe(statusDataToBeUpdated.contents); |
214
|
|
|
expect(result[0].deviceTypes).toEqual(statusDataToBeUpdated.device_types); |
215
|
|
|
expect(result[0].deviceSemVersion).toBe(statusDataToBeUpdated.device_sem_version); |
216
|
|
|
expect(result[0].appSemVersion).toBe(statusDataToBeUpdated.app_sem_version); |
217
|
|
|
expect(moment(result[0].startTime).format()).toBe(statusDataToBeUpdated.start_time); |
218
|
|
|
expect(moment(result[0].endTime).format()).toBe(statusDataToBeUpdated.end_time); |
219
|
|
|
}); |
220
|
|
|
}); |
221
|
|
|
}); |
222
|
|
|
|
223
|
|
|
test('deactivate status', () => { |
224
|
|
|
return serverPromise.then((server) => { |
225
|
|
|
return server.inject({ |
226
|
|
|
url: `${url}/${statusDataToBeAdded.id}/deactivate`, |
227
|
|
|
method: 'PUT', |
228
|
|
|
credentials: { username: 'admin' }, |
229
|
|
|
}).then((response) => { |
230
|
|
|
expect(response.statusCode).toBe(200); |
231
|
|
|
const payload = JSON.parse(response.payload); |
232
|
|
|
expect(payload.success).toBe(true); |
233
|
|
|
expect(payload.data[0].id).toBe(statusDataToBeAdded.id); |
234
|
|
|
return payload; |
235
|
|
|
}).then(payload => Status.find({ _id: payload.data[0].id })) |
236
|
|
|
.then((result) => { |
237
|
|
|
expect(result.length).toBe(1); |
238
|
|
|
expect(result[0].isActivated).toBe(false); |
239
|
|
|
}); |
240
|
|
|
}); |
241
|
|
|
}); |
242
|
|
|
|
243
|
|
|
test('activate status', () => { |
244
|
|
|
return serverPromise.then((server) => { |
245
|
|
|
return server.inject({ |
246
|
|
|
url: `${url}/${statusDataToBeAdded.id}/activate`, |
247
|
|
|
method: 'PUT', |
248
|
|
|
credentials: { username: 'admin' }, |
249
|
|
|
}).then((response) => { |
250
|
|
|
expect(response.statusCode).toBe(200); |
251
|
|
|
const payload = JSON.parse(response.payload); |
252
|
|
|
expect(payload.success).toBe(true); |
253
|
|
|
expect(payload.data[0].id).toBe(statusDataToBeAdded.id); |
254
|
|
|
return payload; |
255
|
|
|
}).then(payload => Status.find({ _id: payload.data[0].id })) |
256
|
|
|
.then((result) => { |
257
|
|
|
expect(result.length).toBe(1); |
258
|
|
|
expect(result[0].isActivated).toBe(true); |
259
|
|
|
}); |
260
|
|
|
}); |
261
|
|
|
}); |
262
|
|
|
|
263
|
|
|
test('remove status', () => { |
264
|
|
|
return serverPromise.then((server) => { |
265
|
|
|
return server.inject({ |
266
|
|
|
url: `${url}/${statusDataToBeAdded.id}`, |
267
|
|
|
method: 'DELETE', |
268
|
|
|
credentials: { username: 'admin' }, |
269
|
|
|
}).then((response) => { |
270
|
|
|
expect(response.statusCode).toBe(200); |
271
|
|
|
const payload = JSON.parse(response.payload); |
272
|
|
|
expect(payload.success).toBe(true); |
273
|
|
|
expect(payload.data[0].id).toBe(statusDataToBeAdded.id); |
274
|
|
|
}); |
275
|
|
|
}); |
276
|
|
|
}); |
277
|
|
|
}); |
278
|
|
|
}); |
279
|
|
|
|